<template>
  <div class="page-container">
    <el-form
      ref="formRef"
      class="form-wrapper"
      :model="formData"
      :rules="rules"
    >
      <div class="title">注册</div>
      <div class="title-tips">
        搜不到我的机构,
        <router-link to="/apply">申请创建</router-link>
      </div>
      <el-row>
        <el-col :span="11">
          <el-form-item label="绑定机构" prop="username">
            <el-autocomplete
              v-model.trim="formData.username"
              v-focus
              :fetch-suggestions="handleQuerySearch"
              placeholder="请输入内容"
              :trigger-on-focus="false"
              @select="handleSelect"
            ></el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="11">
          <el-form-item label="昵称" prop="nickname">
            <el-input
              v-model.trim="formData.nickname"
              type="text"
              placeholder="请输入昵称"
              auto-complete="off"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="帐号" prop="username">
            <el-input
              v-model.trim="formData.username"
              type="text"
              placeholder="请输入账号"
              auto-complete="off"
            />
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="11">
          <el-form-item label="手机号" prop="username">
            <el-input
              v-model.trim="formData.username"
              type="text"
              placeholder="请输入账号"
              auto-complete="off"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="邮箱" prop="email">
            <el-input
              v-model.trim="formData.email"
              type="text"
              placeholder="请输入邮箱"
              auto-complete="off"
            />
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="11"></el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="密码" prop="username">
            <el-input
              v-model.trim="formData.username"
              type="text"
              placeholder="请输入账号"
              auto-complete="off"
            />
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="11">
          <el-form-item label="确认密码" prop="username">
            <el-input
              v-model.trim="formData.username"
              type="text"
              placeholder="请输入账号"
              auto-complete="off"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-button
        :loading="loading"
        class="btn-primary"
        type="primary"
        @click="handleSubmit"
      >
        立即注册
      </el-button>
      <div class="actions">
        <router-link to="/login">已有账号, 立即登录</router-link>
      </div>
    </el-form>
  </div>
</template>
<script>
  import { validateUsername } from '@/utils/validator'
  import { register } from '@/apis/login'
  export default {
    username: 'Register',
    directives: {
      focus: {
        inserted(el) {
          el.querySelector('input').focus()
        },
      },
    },
    data() {
      return {
        formData: {},
        rules: {
          username: [
            { required: true, trigger: 'change', message: '请输入账号' },
            { max: 20, trigger: 'change', message: '最多不能超过20个字' },
            { validator: validateUsername, trigger: 'blur' },
          ],
        },
        loading: false,
      }
    },
    created() {
      document.body.style.overflow = 'hidden'
    },
    beforeDestroy() {
      document.body.style.overflow = 'auto'
    },
    methods: {
      handleSubmit() {
        this.$refs['formRef'].validate(async (valid) => {
          if (valid) {
            const { username, mobile } = this.formData
            await register({
              username,
              mobile,
            })
            this.$baseMessage('操作成功', 'success')
          }
        })
      },
    },
  }
</script>
<style lang="scss" scoped>
  @import './style.scss';

  .page-container {
    width: 1000px;
    margin-top: 70px;

    .actions {
      text-align: center;
    }
  }
</style>
